package avicit.bdp.dds.api.service;

import avicit.bdp.common.base.BaseService;
import avicit.bdp.common.service.dto.TaskSpeedDTO;
import avicit.bdp.dds.dao.entity.TaskLog;
import avicit.bdp.dds.dao.mapper.TaskLogMapper;
import org.springframework.stereotype.Service;

/**
 * @author admin project service
 */
@Service
public class TaskLogService extends BaseService<TaskLogMapper, TaskLog> {

  /**
   * 保存日志
   *
   * @param id id
   * @param contents contents
   */
  public void insertTaskLog(String id, String contents) {
    TaskLog taskLog = new TaskLog();
    taskLog.setId(id);
    taskLog.setContents(contents);
    this.insert(taskLog);
  }

  /**
   * 保存日志
   *
   * @param id id
   * @param contents contents
   */
  public void insertTaskLogOverWrite(String id, String contents) {
    this.mapper.insertTaskLogOverWrite(id, contents);
  }

  /** 保存日志记录速度 */
  public void insertTaskSpeed(
      String id,
      String processInstanceId,
      String taskInstanceId,
      Long stage,
      Long totalStage,
      Long readSucceedRecords,
      Long readSucceedBytes,
      Long readFailedRecords,
      Long readFailedBytes,
      Long writeSucceedRecords,
      Long writeSucceedBytes,
      Long writeFailedRecords,
      Long writeFailedBytes,
      Long endTimestamp) {
    this.mapper.insertTaskSpeed(
        id,
        processInstanceId,
        taskInstanceId,
        stage,
        totalStage,
        readSucceedRecords,
        readSucceedBytes,
        readFailedRecords,
        readFailedBytes,
        writeSucceedRecords,
        writeSucceedBytes,
        writeFailedRecords,
        writeFailedBytes,
        endTimestamp);
  }

  /** 更新日志记录速度 */
  public void updateTaskSpeed(
      String id,
      String processInstanceId,
      String taskInstanceId,
      Long stage,
      Long totalStage,
      Long readSucceedRecords,
      Long readSucceedBytes,
      Long readFailedRecords,
      Long readFailedBytes,
      Long writeSucceedRecords,
      Long writeSucceedBytes,
      Long writeFailedRecords,
      Long writeFailedBytes,
      Long endTimestamp) {
    this.mapper.updateTaskSpeed(
        id,
        processInstanceId,
        taskInstanceId,
        stage,
        totalStage,
        readSucceedRecords,
        readSucceedBytes,
        readFailedRecords,
        readFailedBytes,
        writeSucceedRecords,
        writeSucceedBytes,
        writeFailedRecords,
        writeFailedBytes,
        endTimestamp);
  }

  /** 查询日志记录速度 */
  public TaskSpeedDTO selectOneTaskSpeed(
      String processInstanceId, String taskInstanceId, Long endTimestamp) {
    return this.mapper.selectOneTaskSpeed(processInstanceId, taskInstanceId, endTimestamp);
  }

  /**
   * 是否数据开发的日志
   *
   * @param taskId taskId
   * @return 数据开发的日志
   */
  public boolean isDDSTaskLog(String taskId) {
    int type = this.mapper.getDefinitionTypeByTaskId(taskId);
    if (type == 2) {
      return true;
    } else {
      return false;
    }
  }

  public boolean isExistTaskLog(String taskId) {
    int count = this.mapper.countTaskLog(taskId);
    if (count > 0) {
      return true;
    } else {
      return false;
    }
  }

  public void updateTaskLog(String taskId, String contents) {
    this.mapper.updateTaskLog(taskId, contents);
  }
}
